package com.lh.java.basic.concurrent;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Executor;

public class SerialExecutor {
	  final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
	     final Executor executor;
	     Runnable active;

	     SerialExecutor(Executor executor) {
	         this.executor = executor;
	     }

	     public synchronized void execute(final Runnable r) {
	         tasks.offer(new Runnable() {
	             public void run() {
	                 try {
	                     r.run();
	                 } finally {
	                     scheduleNext();
	                 }
	             }
	         });
	         if (active == null) {
	             scheduleNext();
	         }
	     }

	     protected synchronized void scheduleNext() {
	         if ((active = tasks.poll()) != null) {
	             executor.execute(active);
	         }
	     }

}
